/***************************************************************************
*  Chainflea.cpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "Chainflea.hpp"

 Chainflealayer *Chainflealayer::Getonlyinstance(const old_connect *origconnect,
        const old_recv *origrecv,const old_send *origsend,const old_recvfrom *origrecvfrom,
        const old_sendto *origsendto)
{

        static Chainflealayer chainflea(origconnect,origrecv,origsend,origrecvfrom,origsendto);

        return  &chainflea;
}
const Proxyserver *Chainflealayer::Getproxyserver(const vector<Proxyserver> &availableserver)
{ 
        if(availableserver.size()<1)
              return NULL;
        srand(time(0));

        return &availableserver.at(rand()%(availableserver.size()));
}
int Chainflealayer::Connectflea(const int &sockfd,const  sockaddr *serv_addr,const vector<Proxyserver> &servers)
{

        sockaddr_in  destaddr;
        int ret=-1;
        int i;
        bool actionresult=true;
        ret = Connecttoserver(sockfd,servers.at(0).sServername,servers.at(0).iPort);
        if(ret<0){
         	return ret;
         }
        for(i=1;(i < servers.size())&&(actionresult!=false);i++)
          {
              if(Formaddr_in(servers.at(i).sServername,servers.at(i).iPort,destaddr)==false){
            	      return -1;
                 }
              if(servers.at(i-1).sType=="Socks4")
                 {
            	      actionresult=Actsocks4proxy(sockfd,destaddr);
                 }
              else if(servers.at(i-1).sType=="Socks4a")
                 {
            	      actionresult=Actsocks4aproxy(sockfd,destaddr);
                 }
              else if(servers.at(i-1).sType=="Socks5")
                 {
            	     if((actionresult=Prepareforsocks5proxy(sockfd,servers.at(i-1).sUser,servers.at(i-1).sPasswd))==false){
            	    	     return -1;
            	     }else{
            	    	 actionresult=Actsocks5proxy(sockfd,destaddr);
            	      }
                 }
              else if(servers.at(i-1).sType=="Http1.1")
                 {
            	      actionresult=Acthttpproxy(sockfd,destaddr,servers.at(i-1).sUser,servers.at(i-1).sPasswd);
                 }

              }
            if(actionresult==false){
            	   return -1;
              }
              /// connect to the real destination 
            if(servers.at(i-1).sType=="Socks4"){
              	actionresult=Actsocks4proxy(sockfd,*((const sockaddr_in *)serv_addr));
              }
            else if(servers.at(i-1).sType=="Socks4a"){
            	   actionresult=Actsocks4aproxy(sockfd,*((const sockaddr_in *)(serv_addr)));
              }
           else if(servers.at(i-1).sType=="Socks5"){
                if((actionresult=Prepareforsocks5proxy(sockfd,servers.at(i).sUser,servers.at(i).sPasswd))==false){
                	   return -1;
                   }else{
                	   actionresult=Actsocks5proxy(sockfd,*((const sockaddr_in *)serv_addr));
                   }
              }
            else if(servers.at(i-1).sType=="Http1.1"){
               	actionresult=Acthttpproxy(sockfd,*((const sockaddr_in *)serv_addr),
                        servers.at(i).sUser,servers.at(i).sPasswd);
              }
   	if(actionresult==true){
          return ret;
		}else{
			return -1;
		}
}

Chainflealayer::Chainflealayer(const old_connect *origconnect,const old_recv *origrecv,
          const old_send *origsend,const old_recvfrom *origrecvfrom,const old_sendto *origsendto):
          Lowlayer(origconnect,origrecv,origsend,origrecvfrom,origsendto)
{

}
